﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Collections;

namespace iLikeLite
{
    public partial class frmMain : Form
    {
        private delegate void txtConsoleWriteDelegate(string text);
        private delegate void webBrowserLoadDelegate(string url);
        private delegate void processorDoneDelegate();

        private Crawler crawler = new Crawler();
        private Processor processor = new Processor();
        private Search search = new Search();

        public frmMain()
        {
            InitializeComponent();
        }

        private void btnCrawler_Click(object sender, EventArgs e)
        {
            crawler.CrawlerMessage += new EventHandler(txtConsoleWriteEvent);
            crawler.BrowserLoad += new EventHandler(webBrowserLoadEvent);
            crawler.Start();
            btnStop.Enabled = true;
        }

        private void txtConsoleWrite(string text)
        {
            txtConsole.Text += text + Environment.NewLine;
        }

        private void txtConsoleWriteEvent(object data, EventArgs e)
        {

            if (txtConsole.InvokeRequired)
            {
                txtConsole.Invoke(new txtConsoleWriteDelegate(txtConsoleWrite), new object[] { (string)data });
            }
            else
            {
                txtConsoleWrite((string)data);
            }
        }

        private void webBrowserLoad(string url)
        {
            webBrowser.Navigate(url);
        }

        private void webBrowserLoadEvent(object url, EventArgs e)
        {
            if (webBrowser.InvokeRequired)
            {
                webBrowser.Invoke(new webBrowserLoadDelegate(webBrowserLoad), new object[] { (string)url });
            }
            else
            {
                webBrowserLoad((string)url);
            }
        }

        private void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            if (webBrowser.ReadyState != WebBrowserReadyState.Complete)
            {
                return;
            }
            crawler.loadedHtml = webBrowser.DocumentText;
            crawler.broswerEvent.Set();
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            crawler.stop = true;
            btnStop.Enabled = false;
        }

        private void btnProcessor_Click(object sender, EventArgs e)
        {
            processor.ProcessorDone += new EventHandler(processorDoneEvent);
            processor.Start();
        }

        private void listImageIds_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listImageIds.SelectedIndex >= 0)
            {
                int id = (int)listImageIds.SelectedItem;
                processor.GetImage(id);
                txtTitle.Text = processor.curTitle;
                txtKeywords.Text = processor.curKeywords;
                txtHistogram.Text = processor.curHistogram;
                pictureBox.Image = (Image)processor.curImage.Clone();
            }
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabControl1.SelectedIndex == 1)
            {
                ArrayList ids = processor.GetImageIds();
                listImageIds.Items.Clear();
                foreach (int id in ids)
                {
                    listImageIds.Items.Add(id);
                }
            }
        }

        private void processorDoneNotification()
        {
            MessageBox.Show("Processing complete!");
        }

        private void processorDoneEvent(object sender, EventArgs e)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new processorDoneDelegate(processorDoneNotification));
            }
            else
            {
                processorDoneNotification();
            }
        }

        private void btnSaveInfo_Click(object sender, EventArgs e)
        {
            processor.SaveInfo((int)listImageIds.SelectedItem, txtTitle.Text, txtKeywords.Text);
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            ArrayList ids = search.GetSearchResultIds(txtSearchQuery.Text);
            txtQueryHisto.Text = search.curHistogram;
            listSearchResults.Items.Clear();
            for (int i = 0; i < ids.Count; ++i)
            {
                listSearchResults.Items.Add((int)ids[i]);
            }
        }

        private void listSearchResults_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listSearchResults.SelectedIndex >= 0)
            {
                int id = (int)listSearchResults.SelectedItem;
                processor.GetImage(id);
                txtSearchTitle.Text = processor.curTitle;
                picSearch.Image = (Image)processor.curImage.Clone();
            }
        }

        private void btnSaveImage_Click(object sender, EventArgs e)
        {
            picSearch.Image.Save("picture.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
        }
    }
}
